package cc.abanlee.framework.apilog.config;

import cc.abanlee.framework.apilog.core.filter.ApiAccessLogFilter;
import cc.abanlee.framework.apilog.core.service.ApiAccessLogFrameworkService;
import cc.abanlee.framework.apilog.core.service.ApiErrorLogFrameworkService;
import cc.abanlee.framework.apilog.core.service.impl.ApiAccessLogFrameworkServiceImpl;
import cc.abanlee.framework.apilog.core.service.impl.ApiErrorLogFrameworkServiceImpl;
import cc.abanlee.framework.common.enums.WebFilterOrderEnum;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @author: create by 飞鱼
 * @version: v1.0
 * @description:
 * @date:2022/8/16
 */
@Configuration
public class LiApiLogAutoConfiguration {

    @Bean
    public ApiAccessLogFrameworkService apiAccessLogFrameworkService(){
        return new ApiAccessLogFrameworkServiceImpl();
    }

    @Bean
    public ApiErrorLogFrameworkService apiErrorLogFrameworkService(){
        return new ApiErrorLogFrameworkServiceImpl();
    }

    @Bean
    public FilterRegistrationBean<ApiAccessLogFilter> apiAccessLogFilter(ApiAccessLogFrameworkService apiAccessLogFrameworkService){
        return createFilterBean(new ApiAccessLogFilter(apiAccessLogFrameworkService), WebFilterOrderEnum.API_ACCESS_LOG_FILTER);
    }

    private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
        FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
        bean.setOrder(order);
        return bean;
    }
}
